package com.baozun.houji.me.fragment

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseFragment
import com.baozhun.mall.common.base.mEventViewModel
import com.baozhun.mall.common.ext.loadListData
import com.baozhun.mall.common.model.enums.MainNavigateType
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.util.eventbus.EventBusUtils
import com.baozhun.mall.common.util.eventbus.EventCode
import com.baozhun.mall.common.util.eventbus.EventMessage
import com.baozhun.mall.common.widget.EmptyView
import com.baozun.houji.me.R
import com.baozun.houji.me.adapter.SystemMsgAdapter
import com.baozun.houji.me.databinding.FragmentSystemMsgBinding
import com.baozun.houji.me.viewmodel.SystemMsgViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @Title：系统消息
 * @Description：
 * @Author：xing.luo
 * @Since：2022/12/16
 * @Version：1.0.0
 */
class SystemMsgFragment : BaseFragment<SystemMsgViewModel, FragmentSystemMsgBinding>(), OnRefreshListener, OnLoadMoreListener, OnItemClickListener {

    private val mSysMsgAdapter by lazy { SystemMsgAdapter() }

    companion object {
        fun newInstance(): SystemMsgFragment {
            return SystemMsgFragment().apply {
//                arguments = Bundle().apply { putSerializable(Constants.Common.BUNDLE_NAME, tabType) }
            }
        }
    }


    override fun initView(savedInstanceState: Bundle?) {
        EventBusUtils.register(this)
        mDataBinding.refreshLayout.setOnRefreshListener(this)
        mSysMsgAdapter?.loadMoreModule?.setOnLoadMoreListener(this)
        mSysMsgAdapter.setOnItemClickListener(this)
        mSysMsgAdapter?.setEmptyView(EmptyView.Builder(mActivity).emptyImage(R.drawable.ic_data_empty_message).build())
        mDataBinding.msgRv.adapter = mSysMsgAdapter
    }

    override fun initData() {
        super.initData()
        mViewModel.getSystemMsgList()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.systemMsgList.observe(this) {
            loadListData(it, mSysMsgAdapter, mDataBinding.refreshLayout)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: EventMessage<Any>) {
        if (event.eventCode == EventCode.LOGIN_SUCCESS) {
            mViewModel.getSystemMsgList()
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mViewModel.getSystemMsgList()
    }

    override fun onLoadMore() {
        mViewModel.getSystemMsgList(false)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        var sysMsgModel = mSysMsgAdapter.getItem(position)
        when (sysMsgModel.msg_type) {
            //跳转余额归还计划关联订单列表
            3 -> ARouter.getInstance().build(RoutePath.Me.INTEGRAL_RETURN_PLAN_RELATED_ORDER)
                .withSerializable(Constants.Order.INTEGRAL_RETURN_BILL_ID, sysMsgModel.business_id)
                .navigation()
            //跳转余额归还页
            4 -> ARouter.getInstance().build(RoutePath.Me.INTEGRAL_RETURN_PLAN).navigation()
            //跳转我的-余额明细页
            5 -> ARouter.getInstance().build(RoutePath.Me.INTEGRAL_DETAIL).navigation()
            //跳转退款详情
            6, 7, 8 -> ARouter.getInstance().build(RoutePath.Order.REFUND_DETAIL).withString(Constants.Order.REFUND_ID, sysMsgModel.business_id).navigation()
            9 -> mEventViewModel.switchTabEvent.value = MainNavigateType.ORDER.ordinal
        }
    }
}